﻿Imports VSModel
Imports Cls_Common
Partial Class Control_WhatCookToDay_DetailNewsWhatCookToDay
    Inherits System.Web.UI.UserControl
    Dim iFirstSubMenu As Integer = 0
    Private strFileUrl As String
    Private strUrlYoutube As String
    Private strPlayerUrl As String
    Private strImageUrl As String
    Private strLinkUrl As String
    Public Property ImageUrl() As String
        Get
            Return strImageUrl
        End Get
        Set(ByVal Value As String)
            strImageUrl = Value
        End Set
    End Property
    Public Property LinkUrl() As String
        Get
            Return strLinkUrl
        End Get
        Set(ByVal Value As String)
            strLinkUrl = Value
        End Set
    End Property
    'System.Configuration.ConfigurationManager.AppSettings("player_url")
    Private strVideoTitle As String

    Public Property UrlYoutube() As String
        Get
            Return strUrlYoutube
        End Get
        Set(ByVal Value As String)
            strUrlYoutube = Value
        End Set
    End Property
    Public Property FileUrl() As String
        Get
            Return strFileUrl
        End Get
        Set(ByVal Value As String)
            strFileUrl = Value
        End Set
    End Property
    Public Property PlayerUrl() As String
        Get
            Return strPlayerUrl
        End Get
        Set(ByVal Value As String)
            strPlayerUrl = Value
        End Set
    End Property
    Public Property VideoTitle() As String
        Get
            Return strVideoTitle
        End Get
        Set(ByVal Value As String)
            strVideoTitle = Value
        End Set
    End Property
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Page.RouteData.Values("id") Is Nothing Then
                Dim iMenuId As Integer = Page.RouteData.Values("MenuId")
                Dim iId As Integer = Page.RouteData.Values("id")
                hidID.Value = iId.ToString
                hidMenuId.Value = iMenuId.ToString
                ShowDataDEtail(iMenuId, iId)
                '  Comment1.CommentTypeID = Function_Name.Content
                ' Comment1.TargetID = iId

            End If
        End If
    End Sub
    'Show noi dung object
    Protected Sub ShowDataDEtail(ByVal iMenuID As Integer, ByVal iobjId As Integer)
        Using data As New VSEntities
            Dim p = (From c In data.Contents Where c.Id = iobjId And c.StatusId = 3 Select c).SingleOrDefault
            If Not p Is Nothing Then
                If (Session("PageViewDetailCook") Is Nothing) Then
                    Session("PageViewDetailCook") = Now
                    'Update so luot xem len 1
                    p.Viewed = IIf(IsNothing(p.Viewed) = True, 1, p.Viewed + 1)
                    data.SaveChanges()
                Else
                    If (DateDiff(DateInterval.Second, CType(Session("PageViewDetailCook"), Date), Now()) >= ConfigurationManager.AppSettings("SettingTimeUpdatePageView")) Then
                        'Update so luot xem len 1
                        p.Viewed = IIf(IsNothing(p.Viewed) = True, 1, p.Viewed + 1)
                        data.SaveChanges()
                        Session("PageViewDetailCook") = Now
                    End If
                End If
                ' hplCategory.Text = IIf(IsNothing(p.Menu.Title) = True, "", p.Menu.Title)
                hplTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
                Page.Title = hplTitle.Text & " - " & HeaderTitleSSM
                '   ltrViewAndUpdateDate.Text = "Ngày cập nhật " & CType(p.PublishedDate, DateTime).ToString("dd.MM.yyyy") '"Lượt xem " & IIf(IsNothing(p.Viewed) = True, 1, p.Viewed) & "<br>
                Page.MetaDescription = p.Description

                'Facebook Tag
                'Dim metaTagTitle As New HtmlMeta
                'metaTagTitle.Attributes.Add("property", "og:title")
                'metaTagTitle.Attributes.Add("content", p.Title & " - " & HeaderTitleSSM)
                'Page.Header.Controls.RemoveAt(5)
                'Page.Header.Controls.AddAt(5, metaTagTitle)

                'Dim metaTagUrl As New HtmlMeta
                'metaTagUrl.Attributes.Add("property", "og:url")
                'metaTagUrl.Attributes.Add("content", HttpContext.Current.Request.Url.AbsoluteUri)
                'Page.Header.Controls.RemoveAt(7)
                'Page.Header.Controls.AddAt(7, metaTagUrl)

                'Dim metaTagImg As New HtmlMeta
                'metaTagImg.Attributes.Add("property", "og:image")
                'metaTagImg.Attributes.Add("content", p.Image)
                'Page.Header.Controls.RemoveAt(8)
                'Page.Header.Controls.AddAt(8, metaTagImg)

                'Dim metaTagDesc As New HtmlMeta
                'metaTagDesc.Attributes.Add("property", "og:description")
                'metaTagDesc.Attributes.Add("content", p.Description)
                'Page.Header.Controls.RemoveAt(10)
                'Page.Header.Controls.AddAt(10, metaTagDesc)

                'Show keyword
                Dim strKeyWord As String = ""
                Dim obj = (From ck In data.ContentsKeywords Join k In data.Keywords On ck.KeywordId Equals k.KeywordID Where ck.ContentId = p.Id Order By k.Keyword1 Ascending Select New With {.KeyId = ck.KeywordId, .KeyWord = k.Keyword1, .QueryString = k.QueryString}).ToList
                'Tag
                If obj.Count <> 0 Then
                    hidKeyword.Value = obj.Count
                    tag.Visible = True
                    rptTag.DataSource = obj
                    rptTag.DataBind()
                Else
                    tag.Visible = False
                End If

                For i As Integer = 0 To obj.Count - 1
                    strKeyWord &= obj(i).KeyWord & ";"
                Next
                Page.MetaKeywords = strKeyWord
                'lblCountLike.Text = IIf(p.Like Is Nothing, 0, p.Like)  'CountAmountCommentOfObject(iobjId, Function_Name.Content).ToString
                If Not p.Image Is Nothing Then
                    imgDes.ImageUrl = p.Image.ToString
                End If

                lnkSend.NavigateUrl = "~/Page/Member/SendLink.aspx?strtitle=" & p.Title.ToString & "&strurl=" & Request.Url.ToString & "&keepThis=true&TB_iframe=true&height=420&width=400&modal=true"
                ' lblCountComment.Text = IIf(CountAmountCommentOfObject(p.Id).ToString Is Nothing, "0", CountAmountCommentOfObject(p.Id.ToString).ToString)
                If p.TypeId = 2 Then
                    divVideo.Visible = True
                    imgDes.Visible = False
                    ltrContent.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
                    If p.IsYoutube = True Then
                        pnlVideoUpload.Visible = False
                        pnlYoutube.Visible = True
                        VideoTitle() = p.Title
                        FileUrl() = SplitVideoYoutube(p.URL).Replace("autoplay=f", "autoplay=t")
                        ltrShow.Text = p.URL
                    Else
                        Dim baseUrl As String = Request.Url.GetLeftPart(UriPartial.Authority)
                        VideoTitle() = p.Title
                        PlayerUrl() = baseUrl + ResolveUrl_Video("~/jwplayer/player.swf")
                        FileUrl() = IIf(IsNothing(p.URL) = True, "", p.URL).Replace("autoplay=f", "autoplay=t")
                        ImageUrl() = IIf(IsNothing(p.Image) = True, "", p.Image)
                        pnlVideoUpload.Visible = True
                        pnlYoutube.Visible = False
                    End If
                Else
                    ltrContent.Text = IIf(IsNothing(p.Contents) = True, "", p.Contents)
                End If
                ltrAuthur.Text = IIf(IsNothing(p.Author) = True, "", p.Author)
                'hpllinkFbLike.Attributes.Add("onclick", "return SetVoteContent('" & p.Id & "', '" & lblCountLike.ClientID & "');")
                'Show cac bai viet lien quan
                LoadNewsRelated(1, p.Id, p.MenuId)
            End If

        End Using
    End Sub
    Protected Function SplitVideoYoutube(ByVal strUrl As String) As String
        Dim intStart As Integer = strUrl.IndexOf("=")
        Dim intEnd As Integer = strUrl.IndexOf("&")
        Dim scr As String = strUrl.Substring(intStart + 1, strUrl.Length - intStart - 1)
        Return scr
    End Function
    'Show cac bai viet lien quan
    Protected Sub LoadNewsRelated(ByVal iPage As Integer, ByVal iNewsId As Integer, ByVal iobjId As Integer)
        Using data As New VSEntities
            Dim q = (From ct In data.Contents Where ct.Id <> iNewsId And ct.StatusId = 3 And ct.MenuId = iobjId And ct.TypeId = 1 And ct.PublishedDate <= Now _
                  And (ct.eEffectiveDate >= Now Or ct.eEffectiveDate Is Nothing) Order By ct.Created Descending Select ct).ToList
            Dim r = q.Skip((iPage - 1) * 4).Take(4).ToList
            hidCount.Value = q.Count
            If (q.Count > 0) Then
                Create_Pager(hidCount.Value, iPage, 4, 4)
            End If
            With rptArticalRelate
                .DataSource = r
                .DataBind()
            End With
        End Using
    End Sub

#Region "Bind Data bai viet lien quan"
    Protected Sub rptArticalRelate_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptArticalRelate.ItemDataBound
        Dim p = CType(e.Item.DataItem, Object)
        Dim imgDes As Image = CType(e.Item.FindControl("imgDes"), Image)
        If Not p.Image Is Nothing Then
            imgDes.ImageUrl = p.Image.ToString
        End If

        Dim hplImg As HyperLink = CType(e.Item.FindControl("hplImg"), HyperLink)
        hplImg.NavigateUrl = MainDomain & p.QueryString & "-" & p.MenuId & "-" & p.Id & "sss.html"
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)

        If Not p.Title Is Nothing Then
            hplTitle.Text = p.Title
        End If

        hplTitle.NavigateUrl = MainDomain & p.QueryString & "-" & p.MenuId & "-" & p.Id & "sss.html"
        Dim ltrDes As Label = CType(e.Item.FindControl("ltrDes"), Label)
        If Not p.Description Is Nothing Then
            ltrDes.Text = p.Description.ToString
        End If

    End Sub
#End Region
#Region "Cac thu tuc phan trang bai viet lien quan"
    Protected Sub rptTag_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptTag.ItemDataBound
        If e.Item.ItemIndex >= 0 Then
            Dim hplTag As HyperLink = CType(e.Item.FindControl("hplTag"), HyperLink)
            Dim p = CType(e.Item.DataItem, Object)
            If e.Item.ItemIndex <> (hidKeyword.Value - 1) Then
                hplTag.Text = p.Keyword + ", "
            Else
                hplTag.Text = p.Keyword
            End If
            hplTag.NavigateUrl = MainDomain & "tags/" & p.KeyId & "/" & p.QueryString & ".html"
        End If
    End Sub
    Protected Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        If TotalPage > 1 And Page_Index <> 1 Then
            lnkFirst.Enabled = True
        Else
            lnkFirst.Enabled = False
        End If

        If TotalPage > 1 And Page_Index <> TotalPage Then
            lnkLast.Enabled = True
        Else
            lnkLast.Enabled = False
        End If
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With

    End Sub
    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        lnkTitle.CausesValidation = False
        'dang ky async ko postback
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)

        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.CssClass = "active"
            lnkTitle.Text = e.Item.DataItem
        Else
            ' lnkTitle.CssClass = "pagesLink"
            lnkTitle.Text = e.Item.DataItem
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        LoadNewsRelated(CInt(lnkTile.ToolTip.ToString), hidID.Value, hidMenuId.Value)

    End Sub
    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click

        hidCur_Page.Value = hidCur_Page.Value - 1

        LoadNewsRelated(CInt(hidCur_Page.Value), hidID.Value, hidMenuId.Value)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        LoadNewsRelated(CInt(hidCur_Page.Value), hidID.Value, hidMenuId.Value)
    End Sub
#End Region

End Class
